//

#ifndef STK500PROTOCOL_H_
#define STK500PROTOCOL_H_

#define STK_HW_VER                        1
#define STK_SW_MAJOR                      2
#define STK_SW_MINOR                      0x0a

typedef uint8_t uchar;
typedef uint16_t uint;

//extern uchar buff[282];
extern uchar stk_communicate;

typedef struct stk_mess{
	uchar	start;
	uchar	sequence;
	uchar	sizeH;
	uchar	sizeL;
	uchar	token;
	uchar	cmd;
	uchar	body[];
} stk_mess_t;

typedef struct stk_param{
	uchar	id;
	uchar	val;
}stk_param_t;

void stk_buff_add(uchar);


//**** ATMEL AVR - A P P L I C A T I O N   N O T E  ************************
//*
//* Title:		AVR068 - STK500 Communication Protocol
//* Filename:		command.h
//* Version:		1.0
//* Last updated:	31.01.2005
//*
//* Support E-mail:	avr@atmel.com
//*
//**************************************************************************

// *****************[ STK message constants ]***************************

#define MESSAGE_START                       0x1B        //= ESC = 27 decimal
#define TOKEN                               0x0E

// *****************[ STK general command constants ]**************************

#define CMD_SIGN_ON                         0x01 //ok
#define CMD_SET_PARAMETER                   0x02 //ok
#define CMD_GET_PARAMETER                   0x03 //ok
#define CMD_SET_DEVICE_PARAMETERS           0x04 //no
#define CMD_OSCCAL                          0x05 //ok
#define CMD_LOAD_ADDRESS                    0x06 //ok
#define CMD_FIRMWARE_UPGRADE                0x07 //ok


// *****************[ STK ISP command constants ]******************************

#define CMD_ENTER_PROGMODE_ISP              0x10 //ok
#define CMD_LEAVE_PROGMODE_ISP              0x11 //ok
#define CMD_CHIP_ERASE_ISP                  0x12 //ok
#define CMD_PROGRAM_FLASH_ISP               0x13 //ok
#define CMD_READ_FLASH_ISP                  0x14 //ok
#define CMD_PROGRAM_EEPROM_ISP              0x15 //ok
#define CMD_READ_EEPROM_ISP                 0x16 //ok
#define CMD_PROGRAM_FUSE_ISP                0x17 //ok
#define CMD_READ_FUSE_ISP                   0x18 //ok
#define CMD_PROGRAM_LOCK_ISP                0x19 //ok
#define CMD_READ_LOCK_ISP                   0x1A //ok
#define CMD_READ_SIGNATURE_ISP              0x1B //ok
#define CMD_READ_OSCCAL_ISP                 0x1C //ok
#define CMD_SPI_MULTI                       0x1D //TODO

// *****************[ STK status constants ]***************************

// Success
#define STATUS_CMD_OK                       0x00

// Warnings
#define STATUS_CMD_TOUT                     0x80
#define STATUS_RDY_BSY_TOUT                 0x81
#define STATUS_SET_PARAM_MISSING            0x82

// Errors
#define STATUS_CMD_FAILED                   0xC0
#define STATUS_CKSUM_ERROR                  0xC1
#define STATUS_CMD_UNKNOWN                  0xC9

// *****************[ STK parameter constants ]***************************
#define PARAM_BUILD_NUMBER_LOW              0x80
#define PARAM_BUILD_NUMBER_HIGH             0x81
#define PARAM_HW_VER                        0x90
#define PARAM_SW_MAJOR                      0x91
#define PARAM_SW_MINOR                      0x92
#define PARAM_VTARGET                       0x94
#define PARAM_VADJUST                       0x95
#define PARAM_OSC_PSCALE                    0x96
#define PARAM_OSC_CMATCH                    0x97
#define PARAM_SCK_DURATION                  0x98
#define PARAM_TOPCARD_DETECT                0x9A
#define PARAM_STATUS                        0x9C
#define PARAM_DATA                          0x9D
#define PARAM_RESET_POLARITY                0x9E
#define PARAM_CONTROLLER_INIT               0x9F

// *****************[ STK answer constants ]***************************

#define ANSWER_CKSUM_ERROR                  0xB0

#endif /*STK500PROTOCOL_H_*/
